<template>
  <div class="mainbody">
    <van-swipe class="my-swipe" :autoplay="3000" indicator-color="darkred">
      <van-swipe-item v-for="item in gallery" :key="item.id">
        <img :src="item.img_url" alt="" width="100%" style="dispay: block" />
      </van-swipe-item>
    </van-swipe>
    <Tips></Tips>
    <div class="info">
      <h3>{{ info.name }}</h3>
      <div>{{ info.goods_brief }}</div>
      <p>{{ info.retail_price | filterMoney }}</p>
    </div>
    <van-cell title="展示弹出层" is-link @click="show = true" />
    <div class="attribute">
      <h3>商品参数</h3>
      <ul>
        <li v-for="(item, index) in attribute" :key="index">
          <span>{{ item.name }}</span>
          <section class="van-ellipsis">{{ item.value }}</section>
        </li>
      </ul>
    </div>
    <div class="box" ref="box"></div>
    <div class="title">
      <span>常见问题</span>
    </div>
    <ul class="issue">
      <li v-for="item in issue" :key="item.id">
        <h3>{{ item.question }}</h3>
        <p>{{ item.answer }}</p>
      </li>
    </ul>
    <div class="title">
      <span>大家都在看</span>
    </div>
    <Product :goodsList="goodsList"></Product>
    <van-sku
      ref="sku"
      v-model="show"
      :sku="sku"
      :goods="goods"
      :hide-stock="sku.hide_stock"
    />
    <MyGoodsAction @addToCart="addToCart" :badge="badge"></MyGoodsAction>
  </div>
</template>

<script>
import MyGoodsAction from './MyGoodsAction.vue'
import Tips from '../components/Tips.vue'
import Product from '../components/product.vue'
import {
  GetProductDetailAPI,
  GetAboutApi,
  GoodsCountAPI,
  AddToCartAPI
} from '../request/api'
export default {
  name: 'ProductDetail',
  components: {
    Tips,
    Product,
    MyGoodsAction
  },
  data () {
    return {
      // sku的一些属性
      show: false,
      sku: {
        // 商品的树性结构
        tree: [],
        price: '',
        stock_num: '', // 商品总库存
        hide_stock: false // 是否隐藏剩余库存
      },
      goods: {
        // 数据结构见下方文档
        picture: ''
      },
      // 轮播图
      gallery: [],
      // 商品信息
      info: {},
      // 商品参数
      attribute: [],
      // 常见问题
      issue: [],
      // 相关产品
      goodsList: [],
      // 购物车数据
      badge: '',
      productList: []
    }
  },
  created () {
    // console.log(this.$route)
    let param = {
      id: this.$route.query.id
    }
    // 获取产品明细信息
    GetProductDetailAPI(param).then(res => {
      // console.log(res)
      if (res.errno == 0) {
        let { gallery, info, attribute, issue, productList } = res.data
        this.gallery = gallery
        this.info = info
        this.attribute = attribute
        this.productList = productList
        this.issue = issue
        this.$refs.box.innerHTML = this.info.goods_desc
        // 渲染sku
        this.goods.picture = info.list_pic_url
        this.sku.price = info.retail_price.toFixed(2)
        this.sku.stock_num = info.goods_number
      }
    })
    // 获取相关产品
    GetAboutApi(param).then(res => {
      if (res.errno == 0) {
        // console.log('相关产品', res)
        let { goodsList } = res.data
        this.goodsList = goodsList
      }
    })
    // 获取购物车数量
    GoodsCountAPI().then(res => {
      if (res.errno == 0) {
        // console.log('购物车数量接口', res)
        this.badge = res.data.cartTotal.goodsCount
      }
    })
  },
  methods: {
    addToCart () {
      if (this.show) {
        // 这里是购物车面板已经显示，然后需要执行的操作
        // 获取到自己添加的数据，然后就拿这些参数去发送请求
        // console.log(this.$refs.sku.getSkuData().selectedNum)
        let params = {
          goodsId: this.$route.query.id,
          productId: this.productList[0].id,
          number: this.$refs.sku.getSkuData().selectedNum
        }
        AddToCartAPI(params).then(res => {
          if (res.errno == 0) {
            // console.log(res.data)
            this.badge = res.data.cartTotal.goodsCount
          }
        })
      } else {
        this.show = true
      }
    }
  }
}
</script>

<style lang="less" scoped>
.mainbody {
  padding-bottom: 50px;
}
.info {
  background: #fff;
  line-height: 40px;
  text-align: center;
  padding: 20px 0;
  border-bottom: 1px solid #ccc;
  h3 {
    font-size: 20px;
  }
  div {
    color: #666;
  }
  p {
    color: darkred;
  }
}
.attribute {
  padding: 10px 4%;
  margin-top: 20px;
  background: #fff;
  h3 {
    height: 0.5rem;
    line-height: 0.5rem;
  }
  ul {
    li {
      background: #efefef;
      line-height: 0.4rem;
      height: 0.4rem;
      margin-bottom: 0.1rem;
      border-radius: 4px;
      display: flex;
      justify-content: space-between;
      span {
        width: 30%;
        text-align: right;
      }
      section {
        flex: 1;
        padding-left: 0.2rem;
      }
    }
  }
}
/deep/.box {
  img {
    width: 100%;
    display: block;
  }
}
.title {
  width: 100%;
  height: 0.5rem;
  background-color: #fff;
  position: relative;
  &::after {
    content: '';
    height: 2px;
    width: 50%;
    background: #ccc;
    position: absolute;
    top: 50%;
    margin-top: -1px;
    left: 50%;
    margin-left: -25%;
  }
  span {
    width: 30%;
    background: #fff;
    display: block;
    text-align: center;
    height: 0.5rem;
    line-height: 0.5rem;
    margin: 0 auto;
    z-index: 99;
    position: relative;
  }
}
.issue {
  background: #fff;
  padding: 0 4%;
  li {
    h3 {
      height: 0.3rem;
      background: #fff;
      line-height: 0.3rem;
      padding-left: 0.2rem;
      font-weight: normal;
      position: relative;
      margin-bottom: 0.1rem;
      &::before {
        content: '';
        width: 4px;
        height: 4px;
        background: darkred;
        display: inline-block;
        border-radius: 50%;
        position: absolute;
        top: 50%;
        margin-top: -2px;
        left: 0;
      }
    }

    p {
      font-size: 0.12rem;
      padding-left: 0.2rem;
      line-height: 0.2rem;
      padding-bottom: 0.1rem;
    }
  }
}
.van-goods-action {
  z-index: 9997;
}
.van-sku-container {
  min-height: auto;
}
</style>
